<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title>登录</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/common.css" />
		<link rel="stylesheet" type="text/css" href="css/index.css" />
	</head>

	<body class="bg_ff">
		<div id="app">
			<div class="text-center">
				<img class="logo" src="images/logo.png" />
			</div>
			<div class="login_nav text-center">
				<a href="javascript:void(0);" class="active" data-type='2'>密码登录</a>
				<a href="javascript:void(0);" data-type='1'>验证码登录</a>
			</div>
			<div class="form">
				<div class="input_list">
					<input type="text" name="" id="phone" value="" placeholder="请输入账号" />
				</div>
				<div class="input_list psd_login active">
					<input type="password" name="" id="password" value="" placeholder="请输入密码" />
				</div>
				<div class="input_list code_login relative">
					<input type="text" name="" id="" value="" placeholder="请输入验证码" />
					<input type="text" name="" id="" class="getcode absolute" value="获取验证码" readonly="readonly" />
				</div>
				<p class="text_right mg_top20">
					<a href="reg.html" class="color_3">去注册></a>
				</p>
				<p class="text-center mg_top20">
					<a href="javascript:void(0)" class="big_btn" onclick="login()">登录></a>
				</p>
			</div>
			<div class="text-center">
				<a href="javascript:void(0);" id="wxdenglu">
					<img class="wx_login" src="images/icon_wechat.png" />
					<p>微信快捷登录</p>

				</a>
			</div>

			<p class="xy text-center mg_top10 padding_tb20">
				<label for="checkbox" class="label"><img src="images/check_s.png"/>我同意</label>
				<a href="codeRule.html?type=1">《拍拍拍用户使用协议》</a>
				<input type="checkbox" name="" checked="checked" id="checkbox" hidden="hidden" value="" />

			</p>
		</div>

		<div class="msg-spring"></div>
		<script src="js/mui.js"></script>
		<script src="js/jquery-1.7.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/login.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			mui.init()
			if(localStorage.getItem('userId')) {
				window.location.href = 'index.html'
			}
            //微信授权登录
			function wechat(name,wxMark,headimgurl) {
				
				$.ajax({
					type: "post",
					url: urlcom + "user/loginThird",
					async: true,
					dataType: 'json',
					data: {
						name: name,
						headImg: headimgurl,
						wxMark: wxMark
					},
					success: function(res) {
						    console.log(JSON.stringify(res.data))
						if(res.code==0){
							
							window.localStorage.setItem('userId', res.data.userId);
							window.localStorage.setItem('token', res.data.token);
							window.localStorage.setItem('rongToken', res.data.rongToken);
							msg('登录成功')
							
							setTimeout(function() {
								window.location.href = 'like.html'
							}, 1000)
							
						}

					}
				});
			}
             // 普通登录
			function login() {
				var type = $(".login_nav a.active").attr('data-type')
				console.log(type)
				var flage = $("input[type='checkbox']").is(':checked')
				if(flage == false) {
					msg('请选择服务协议')
					return false;
				}
				$.ajax({
					type: "post",
					url: urlcom+"user/userLogin",
					async: true,
					dataType: 'json',
					data: {
						phone: $('#phone').val(),
						type: type,
						password: $('#password').val()
					},
					success: function(res) {
						if(res.code == 0) {
							window.localStorage.setItem('userId', res.data.userId);
							window.localStorage.setItem('token', res.data.token);//token
							window.localStorage.setItem('rongToken', res.data.rongToken);//融云token
							window.localStorage.setItem('inviteCode', res.data.inviteCode);//邀请码
							window.localStorage.setItem('isAnswer', res.data.isAnswer);//是否回答
							msg('登录成功')

							setTimeout(function() {
								window.location.href = 'like.html'
							}, 1000)
						} else {
							msg(res.msg)

						}

					}
				});
			}
			
			
			// 微信授权登录对象
			// 调用plus.oauth.getServices获取保存
			var aweixin=null;    
			// 当前环境支持的所有授权登录对象
			var auths = {};
			
			mui.plusReady(function(){
			           // 获取鉴权服务
			          getService()
			});
			
			//wx绑定
			function weixinBind(){
			    plus.nativeUI.showWaiting("正在登录中...",{padlock: true});
				 console.log(JSON.stringify(aweixin))
			    if(!aweixin.authResult){
			        authorize().then(res=>{
			            wechat(aweixin.userInfo.nickname,aweixin.authResult.openid,aweixin.userInfo.headimgurl)
					 plus.nativeUI.closeWaiting()
			        }).catch(res=>{
			            plus.nativeUI.closeWaiting();
			        })
			    }else{
			        wechat(aweixin.userInfo.nickname,aweixin.authResult.openid,aweixin.userInfo.headimgurl)
						 plus.nativeUI.closeWaiting()
			    }
			};
			
			//wx解绑
			function weixinUnBind(){
			    plus.nativeUI.showWaiting("正在解绑中...",{padlock: true});
			    wechat("")
			}
			
			// 获取登录授权认证服务列表，单独保存微信登录授权对象
			// 5+APP在plusready事件中调用，uni-app在vue页面的onLoad中调用
			function getService(){
			    plus.oauth.getServices(function(services){
			        for(var i=0;i<services.length;i++){
			            auths[services[i].id] = services[i];
			        }
			        aweixin = auths['weixin'];
			        console.log(JSON.stringify(aweixin))
					
			    }, function(e){
			        plus.nativeUI.alert("获取登录授权服务列表失败："+JSON.stringify(e));
			    } );
			}
			
			// 获取微信登录授权对象后可进行授权操作
			function authorize(){
			    return new Promise(function (resolve, reject) {
			        if(!aweixin){
			            plus.nativeUI.alert("当前环境不支持微信登录");
			            return;
			        }
			        aweixin.authorize(function(e){
			            // plus.nativeUI.alert("授权成功："+JSON.stringify(e));
			            authLogin().then(res=>{
			                resolve(res);
			            });
			        }, function(e){
			            // plus.nativeUI.alert("授权失败："+JSON.stringify(e));
			            plus.nativeUI.closeWaiting();
			            reject(e);
			        }, {scope:'snsapi_userinfo',state:'authorize test'});
			    })
			}
			
			// 获取微信登录授权对象后可进行登录认证操作
			function authLogin(){
			    return new Promise(function (resolve, reject) {
			        if(!aweixin){
			            plus.nativeUI.alert("当前环境不支持微信登录");
			            return;
			        }
			        if(!aweixin.authResult){
			            aweixin.login(function(e){
			                // plus.nativeUI.alert("登录认证成功!"+JSON.stringify(e));
			                resolve(e);
			            }, function(e){
			                reject(e);
			            } );
			        }else{
			            // plus.nativeUI.alert("已经登录认证!");
			        }
			    })
			}
			// wx登录
			$('#wxdenglu').click(function(){
				 weixinBind()
			})
			$('.getcode').click(function(){
				if($('#phone').val()==''){
					msg('请输入手机号')
					return false
				}
				$.ajax({
					type:"post",
					url:urlcom + "sms/getSmsCode",
					async:true,
					dataType:'json',
					data:{
						type:1,
						phone:$('#phone').val()
					},
					success:function(res){
						if(res.code==0){
							 msg('验证码发送成功')
							settime() 
						}
						console.log(res)
					}
				});
			})
			

			$(function() {
				$(".login_nav").on('click', 'a', function() {
					$(this).addClass('active').siblings().removeClass('active')
					var index = $(this).index() + 1
					$('.form .input_list').eq(index).addClass('active').siblings().removeClass('active')
				})
				$('#checkbox').click(function() {
					var flage = $("input[type='checkbox']").is(':checked')
					console.log(flage)
					if(flage == true) {
						$(".xy").children('label').find('img').attr('src', 'images/check_s.png')
					} else {
						$(".xy").children('label').find('img').attr('src', 'images/check.png')
					}
				})
				
			})
		</script>
	</body>

</html>